export const inView = {
  mounted(el, binding) {
    const callback = binding.value;
    if (typeof callback !== "function") {
      console.warn("v-in-view 指令的值必须是一个函数");
      return;
    }

    const observer = new IntersectionObserver(
      ([entry]) => {
        callback(entry.isIntersecting);
      },
      {
        threshold: 0.1,
      }
    );
    //监听元素
    observer.observe(el);

    el._intersectionObserver = observer;
  },
  //卸载
  unmounted(el) {
    if (el._intersectionObserver) {
      el._intersectionObserver.disconnect();
      delete el._intersectionObserver;
      el._intersectionObserver = null;
    }
  },
};
